package edu.gatech.oad.mule;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

import edu.gatech.oad.mule.model.GameDriver;

/**
 * This class is the lower most JPanel of the configuration
 * GUI.  It houses the start button and the reset button.
 * The start button begins the game and the reset button
 * resets all the components to their original values
 * 
 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin Park
 * @version %I%, %G%
 */
public class LowerConfigPanel extends JPanel{
	
	//start the game once they put in config data
	private JButton start = new JButton("START");
	
	//load a saved game
	private JButton load = new JButton("LOAD");
	private JFileChooser file = new JFileChooser();

	//instance of the overall container
	private ConfigurationPanel cp;

	/**
	 * Creates a new LowerConfigPanel object
	 */
	public LowerConfigPanel(ConfigurationPanel cp){
		System.out.println("lowerConfigPanel");		
		//store the instance
		this.cp = cp;

		//give this guy a border so we have a little breathing room
		//  from the other JPanels
		setBorder(new EmptyBorder(
				ConfigurationPanel.PANEL_PADDING,
				ConfigurationPanel.PANEL_PADDING,
				ConfigurationPanel.PANEL_PADDING,
				ConfigurationPanel.PANEL_PADDING
				));

		//create the liseners for the buttons
		start.addActionListener(new StartListener());
		load.addActionListener(new LoadListener());

		//add the start, load buttons
		add(start);
		add(load);
		System.out.println("/lowerConfigPanel");
	}//end constructor

	/**
	 * This listener validates all of the input and if everything checks
	 * out, we start the game!
	 * 
	 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin Park
	 * @version %I%, %G%
	 */
	private class StartListener implements ActionListener{		
		@Override
		public void actionPerformed(ActionEvent e){
			cp.validateInput();
		}//end method actionPerformed
	}//end inner class startListener
	
	/**
	 * Listener to load a saved game file
	 * 
	 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin Park
	 * @version %I%, %G%
	 */
	private class LoadListener implements ActionListener{		
		@Override
		public void actionPerformed(ActionEvent e){
			//show a file chooser dialog
			file.showOpenDialog(null);
			//make sure they actually selected something
			if(file.getSelectedFile() != null){
				System.out.println(file.getSelectedFile().getName());
				//if the load happened successfully, hide the config GUI
				cp.runSavedGame(file.getSelectedFile());
			}//end if
		}//end method actionPerformed
	}//end inner class startListener

}//end class LowerConfigPanel
